// 连接数据库
const request = require('request')
const getConnection = require('../database/mySql_Utils')

var resp = {
    result: 0,
    msg: "",
    data: "",
};

// 获取微信用户的openId
function getOpenId(req, res) {
    let code = req.query.code
    let appId = 'wxfe1682986fee7423'
    let appsecret = '98e4a72ec97ef1e9186c4ff91fa479e8'
    request('https://api.weixin.qq.com/sns/jscode2session?appid='
        + appId
        + '&secret='
        + appsecret
        + '&js_code='
        + code
        + '&grant_type=authorization_code',
        { json: true },
        (err, resp, body) => {
            if (err) {
                return console.log(err)
            }
            const openid = body.openid
            res.send(openid)
        })
}

// 查询数据库里的乘客openId是否存在
function getUserOpenId(req, res) {
    let openid = req.query.openid
    console.log(openid);
    const mysql = getConnection()
    let sql = 'select userId,phoneNum,headPortrait,credit,name,wxName from usertable WHERE openId=?'
    mysql.query(sql, [openid], (err, data) => {
        if (err) {
            resp.result = -1,
                resp.msg = '失败'
        } else {
            if (data.length > 0) {
                resp.result = 1
                resp.msg = '用户已存在'
                resp.data = data
            } else {
                resp.result = 0
                resp.msg = '用户不存在'
                resp.data = data
            }
        }
        console.log(data, 42)
        res.send(resp)
        mysql.end()
    })

}

// 添加乘客用户
function userTable(req, res) {
    let userTel = req.query.userTel
    let openid = req.query.openid
    let avatarUrl = req.query.avatarUrl
    let userInfo = req.query.userInfo
    const mysql = getConnection()
    let sql = 'select phoneNum from usertable WHERE phoneNum=?'
    mysql.query(sql, [userTel], (err, data) => {
        if (err) {
            resp.result = -1,
                resp.msg = '失败'
            res.send(resp)
            mysql.end()
        } else {
            console.log(data.length, 1)
            if (data.length > 0) {
                resp.result = 0
                resp.msg = '手机号已被注册'
                console.log(data, '80')
                res.send(resp)
                mysql.end()
            } else {
                resp.result = 1
                resp.msg = '手机号不重复'
                let sql1 = 'insert into usertable(phoneNum,openId,headPortrait,credit,balance,establishTime,wxName)values(?,?,?,5,0,now(),?) '
                mysql.query(sql1, [userTel, openid, avatarUrl, userInfo], (err, data) => {
                    if (err) {
                        resp.result = -1,
                            resp.msg = '失败'
                        console.log('2')
                        res.send(resp)
                        mysql.end()
                    } else {
                        resp.result = 1,
                            // resp.data = data
                            resp.msg = '添加成功'
                        let sql2 = 'select userId,phoneNum,headPortrait,credit,name,wxName from usertable WHERE openId=?'
                        mysql.query(sql2, [openid], (err, data) => {
                            if (err) {
                                resp.result = -1,
                                    resp.msg = '失败'
                                console.log('3')
                                res.send(resp)
                                mysql.end()
                            } else {
                                resp.result = 1
                                resp.msg = '成功'
                                resp.data = data
                                console.log('4')
                                res.send(resp)
                                mysql.end()
                            }
                        })
                    }
                })
            }
        }
        // console.log(data,88)
    })
}

// 查询数据库里的乘客openId是否存在
function userTel(req, res) {
    let openid = req.query.openid
    // console.log(openid)
    const mysql = getConnection()
    let sql = 'select userId,phoneNum,headPortrait,credit,name,wxName from usertable WHERE openId=?'
    mysql.query(sql, [openid], (err, data) => {
        if (err) {
            resp.result = -1,
                resp.msg = '失败'
        } else {
            if (data.length > 0) {
                resp.result = 1
                resp.msg = '用户已存在'
                resp.data = data
            } else {
                resp.result = 0
                resp.msg = '用户不存在'
                resp.data = data
            }
        }
        // console.log(data,42)
        res.send(resp)
        mysql.end()
    })

}

// 查询司机是否首次登录
function DriverUser(req, res) {
    let openid = req.query.openid
    console.log(openid)
    const mysql = getConnection()
    let sql = "select driverId,phoneNumber,driverName,driverPhoto,CreditPoints,driverName,auditState from driveruser WHERE openId=? and auditState!='36'"
    mysql.query(sql, [openid, 36], (err, data) => {
        if (err) {
            resp.result = -1,
                resp.msg = '失败'
        } else {
            if (data.length > 0) {
                resp.result = 1,
                    resp.data = data
                resp.msg = '司机不为首次登录'
            } else {
                resp.result = 0,
                    resp.data = data
                resp.msg = '司机为首次登录'
            }

        }
        // console.log(data,99)
        res.send(resp)
        mysql.end()
    })
}

// 查询司机电话号码
function DriverPhoneNum(req, res) {
    let openid = req.query.openid
    let phoneNumber = req.query.phoneNum
    let avatarUrl = req.query.avatarUrl
    console.log(openid)
    const mysql = getConnection()
    let sql = "select driverId,phoneNumber,driverName,driverPhoto,CreditPoints,driverName,auditState from driveruser WHERE phoneNumber=? and auditState!='36'"
    mysql.query(sql, [phoneNumber], (err, data) => {
        if (err) {
            resp.result = -1,
                resp.msg = '失败'
            res.send(resp)
            mysql.end()
        } else {
            console.log(data.length)
            if (data.length > 0) {
                resp.result = 1
                resp.msg = '司机手机已存在'
                resp.data = data
                let sql1 = "UPDATE driveruser SET openId=?,driverPhoto=? where phoneNumber=? and auditState='11'"
                mysql.query(sql1, [openid, avatarUrl, phoneNumber], (err, data) => {
                    if (err) {
                        resp.result = -1,
                            resp.msg = '失败'
                        res.send(resp)
                        mysql.end()
                    } else {
                        resp.result = 1,
                            // resp.data = data
                            resp.msg = '成功'
                        console.log('1111')
                        res.send(resp)
                        mysql.end()
                    }
                })
            } else {
                resp.result = 0
                resp.msg = '司机手机不存在'
                // resp.data = data
                res.send(resp)
                mysql.end()
            }
        }
        // console.log(resp,199)
    })
}

module.exports = {
    getOpenId,
    getUserOpenId,
    userTable,
    userTel,
    DriverUser,
    DriverPhoneNum
}